package cn.li.security.json.handle;

import cn.li.security.json.bean.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;

//无登陆用户访问需权限资源时的异常
//http.exceptionHandling().authenticationEntryPoint(new NoneAuthenticationJSONHandler())
public class NoneAuthenticationJSONHandler
        extends BaseJSONHandler
        implements AuthenticationEntryPoint, Serializable {

    private static final long serialVersionUID = -8970718410437077606L;

    public NoneAuthenticationJSONHandler(ObjectMapper objectMapper) {
        super(objectMapper);
    }

    @Override
    public void commence(HttpServletRequest request,
                         HttpServletResponse response,
                         AuthenticationException authException) throws IOException {
       Result result = new Result(HttpServletResponse.SC_FORBIDDEN,"Access Denied",null);
       this.writeResult(response , result);
    }


}